﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace StuffNET.Forms
{
    public class ProgressForm
    {
        private _ProgressForm progressForm;

        /// <summary>
        /// The value of the progressbar. The value has to be between 0 and 100.
        /// </summary>
        public int Value
        {
            get
            {
                return progressForm.progressBar.Value;
            }
            set
            {
                if (value >= 101)
                {
                    progressForm.progressBar.Value = 99;
                    progressForm.progressBar.Value = 100;
                    progressForm.progressBar.Refresh();
                    Thread.Sleep(900);
                    progressForm.Close();
                    return;
                }
                value = value < 0 ? 0 : value;
                progressForm.progressBar.Value = value - 1 < 0 ? 0 : value - 1;
                progressForm.progressBar.Value = value;
                progressForm.infoLabel.Text = value + "%";

            }
        }

        /// <summary>
        /// Gets whether the form is still open
        /// </summary>
        public bool IsOpen { get; private set; }

        /// <summary>
        /// Gets or sets an infotext for the current operation
        /// </summary>
        public string CurrentOperationInfo
        {
            get
            {
                return progressForm.currentStepInfoLabel.Text;
            }
            set
            {
                progressForm.currentStepInfoLabel.Text = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title">The title of the progressbar containing form</param>
        /// <param name="style">The style of the progressbar. If "ProgressBarStyle.Marquee" is passed, no progressinfo will be displayed</param>
        /// <param name="startValue">The current value</param>
        public ProgressForm(string title, ProgressBarStyle style, int startValue)
        {
            progressForm = new _ProgressForm();
            progressForm.FormClosed += progressForm_FormClosed;
            progressForm.Text = title;
            progressForm.progressBar.Style = style;
            progressForm.progressBar.Value = startValue;
            if (style == ProgressBarStyle.Marquee)
                progressForm.infoLabel.Visible = false;
            progressForm.Show();
            IsOpen = true;
        }

        internal void progressForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            IsOpen = false;
        }
    }
}
